package cn.bestick.cblog.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bestick.cblog.dto.MsgDto;
import cn.bestick.cblog.service.MsgService;
import cn.bestick.cblog.service.impl.MsgServiceImpl;

public class MsgManageServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private MsgService msgService = new MsgServiceImpl();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");

		String action = request.getParameter("action");
		if ("hidden".equals(action)) {
			hidden(request, response);
		} else if ("delete".equals(action)) {
			delete(request, response);
		} else {
			list(request, response);
		}
		request.getRequestDispatcher("manage.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) {
		List<MsgDto> msgs = msgService.getAll();
		request.setAttribute("msgs", msgs);
		request.setAttribute("mainPage", "template/msg/manage-list.jsp");
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) {
		String msgId = request.getParameter("msgId");
		msgService.deleteMsg(Integer.parseInt(msgId));
		List<MsgDto> msgs = msgService.getAll();
		request.setAttribute("msgs", msgs);
		request.setAttribute("mainPage", "template/msg/manage-list.jsp");
	}

	private void hidden(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
	}
}
